package com.coolw.file.controller;

import com.coolw.file.domain.response.AgreementResponse;
import com.coolw.file.util.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * html控制层
 *
 * @author coolw
 * @date 2024/2/27 10:26
 */
@RequestMapping("/html")
@RestController
public class HtmlController {

    private final String aesKey = "oJr54Phgk58E9pfwaw6GCA==";

    @Autowired
    private TemplateEngine templateEngine;

    @GetMapping("/build")
    public List<AgreementResponse> buildHtml() throws Exception {
        List<AgreementResponse> list = new ArrayList<>();
        list.add(new AgreementResponse("《桔多多用户注册协议》", "https://shopshop.juzifenqi.com/register_protocol.html"));
        list.add(new AgreementResponse("《桔多多隐私协议》", "https://shopshop.juzifenqi.com/AgreementResponse/protocol_PrivacyProtection.html"));
        list.add(new AgreementResponse("《桔多多平台服务协议》", "https://shopshop.juzifenqi.com/AgreementResponse/AgreementResponse-list.html"));

        // 创建上下文对象
        Context context = new Context();
        context.setVariable("AgreementResponseList", list);

        // 渲染模板
        String htmlContent = templateEngine.process("AgreementResponse-list", context);

        String encrypt = AESUtil.encrypt(htmlContent, aesKey);

        String url = "localhost:8080/html/open?content=" + URLEncoder.encode(encrypt, "utf-8");

        List<AgreementResponse> AgreementResponseList = new ArrayList<>();
        AgreementResponseList.add(new AgreementResponse("授信相关协议", url));
        return AgreementResponseList;
    }

    @GetMapping("/build1")
    public List<AgreementResponse> buildHtml1() throws Exception {
        // 读取html模板内容
        String htmlContent = AESUtil.readFile("templates/AgreementResponse.html");

        String encrypt = AESUtil.encrypt(htmlContent, aesKey);

        String url = "localhost:8080/html/open?content=" + URLEncoder.encode(encrypt, "utf-8");

        List<AgreementResponse> AgreementResponseList = new ArrayList<>();
        AgreementResponseList.add(new AgreementResponse("授信相关协议", url));
        return AgreementResponseList;
    }

    @GetMapping("/open")
    public void openHtml(@RequestParam String content, HttpServletResponse response) throws Exception {
        String htmlContent = AESUtil.decrypt(content, aesKey);

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(htmlContent);
    }

    @GetMapping("/test")
    public void test(HttpServletResponse response) throws Exception {
        List<AgreementResponse> AgreementResponseList = new ArrayList<>();
        AgreementResponseList.add(new AgreementResponse("桔多多用户注册协议", "https://shopshop.juzifenqi.com/register_protocol.html"));
        AgreementResponseList.add(new AgreementResponse("桔多多隐私协议", "https://shopshop.juzifenqi.com/AgreementResponse/protocol_PrivacyProtection.html"));
        AgreementResponseList.add(new AgreementResponse("桔多多平台服务协议", "https://shopshop.juzifenqi.com/AgreementResponse/AgreementResponse-list.html"));

        // 创建上下文对象
        Context context = new Context();
        context.setVariable("AgreementResponseList", AgreementResponseList);

        // 渲染模板，并将渲染后的HTML内容返回给前端
        String htmlContent = templateEngine.process("AgreementResponse-list", context);

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(htmlContent);
    }
}
